/*
  Jquery Validation using jqBootstrapValidation
   example is taken from jqBootstrapValidation docs 
  */
$(function() {
 
    $("input,select,file").jqBootstrapValidation({
 
        preventSubmit: true,
        submitError: function($form, event, errors) {
 
            // something to have when submit produces an error ?
            // Not decided if I need it yet
         
        },
        submitSuccess: function($form, event) {
 
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var pname = $("input#pname").val();
            var type1 = $("select#type1").val();
            var type2 = $("input:radio[name=type2]:checked").val();
            var unit = $("input#unit").val();
            var contact = $("input#contact").val();
            var phone = $("input#phone").val();
            var email = $("input#email").val();
            var address = $("input#address").val();
            // var file = $("input#file").val();
            var file = document.sentMessage.file.files[0];
            var yzm = $("input#yzm").val();
            // 有规划选项才能有其他选项。
            // if (type1 === "6") {
            //     var type2 = $("select#type2").val();
            // }
            // var temp = pname + '|' + type1 + '|' + unit + '|' + contact + '|' + phone + '|' + email + '|' + address + '|' + yzm + '|' + type2
            // console.log(temp);
            var fm = new FormData();
            fm.append('pname', pname);
            fm.append('type1', type1);
            fm.append('type2', type2);
            fm.append('unit', unit);
            fm.append('contact', contact);
            fm.append('phone', phone);
            fm.append('email', email);
            fm.append('file', file);
            fm.append('yzm', yzm);
            $.ajax({
                url: "/index/Index/upload",
                type: "POST",
                data: fm,
                processData:false,//禁止jquery对数据处理，默认会处理，禁止的原因，FormData已经帮我们做了处理！
                cache: false,
                success: function() {
 
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');
 
                    //clear all fields
                    $('#contactForm').trigger("reset");
                 
                },
                error: function() {
 
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + " it seems that my mail server is not responding...</strong> Could you please email me directly to <a href='mailto:me@example.com?Subject=Message_Me from myprogrammingblog.com;>me@example.com</a> ? Sorry for the inconvenience!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                 
                },
             
            })
         
        },
        filter: function() {
 
            return $(this).is(":visible");
         
        },
     
    });
 
    $("a[data-toggle=\"tab\"]").click(function(e) {
 
        e.preventDefault();
        $(this).tab("show");
     
    });
 
});
 
 
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
 
    $('#success').html('');
 
});